package com.liu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liu.eduservice.entity.EduSubject;
import com.liu.eduservice.entity.excel.SubjectDate;
import com.liu.eduservice.entity.subject.OneSubject;
import com.liu.eduservice.entity.subject.TwoSubject;
import com.liu.eduservice.listener.SubjectExcelListener;
import com.liu.eduservice.mapper.EduSubjectMapper;
import com.liu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author achang
 * @since 2022-05-16
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService eduSubjectService) {


        // 读取文件在哪都要考虑异常
        try {

            // 创建输入的管道流
            InputStream inputStream = file.getInputStream();

            // 操作excel 参数的意义(管道流，需要读取的对象，监听器)
            EasyExcel.read(inputStream, SubjectDate.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20002, "添加课程分类失败");
        }


    }

    // 查询所有edu_subject表中一级二级分类的对象
    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        // 查询所有一级分类(parent_id = 0)
        QueryWrapper<EduSubject> oneSubjectQueryWrapper = new QueryWrapper<>();
        oneSubjectQueryWrapper.eq("parent_id", "0");
        List<EduSubject> oneSubjectQueryList = baseMapper.selectList(oneSubjectQueryWrapper);

        // 查询所有二级分类（parent_id！= 0）
        QueryWrapper<EduSubject> twoSubjectQueryWrapper = new QueryWrapper<>();
        twoSubjectQueryWrapper.ne("parent_id", "0");
        List<EduSubject> twoSubjectQueryList = baseMapper.selectList(twoSubjectQueryWrapper);


        //创建list集合，用于封装最终数据
        List<OneSubject> oneFinalSubjectList = new ArrayList<>();


        //封装一级分类
        // 由于查询出来的oneSubjectQueryObject对象，泛形是EduSubject 不是我们需要的OneSubject对象格式
        // 遍历oneSubjectQueryList对象 取出我们需要的属性付给OneSubject  在封装在一个新的List<OneSubject> finalSubjectList中
        // oneSubjectQueryList = [{},{},{},{}]
        for (int i = 0; i < oneSubjectQueryList.size(); i++) {

            // 得到oneSubjectQueryObject中每个eduSubject对象
            EduSubject oneEduSubjectObject = oneSubjectQueryList.get(i);

            //把eduSubject里面值获取出来，放到oneSubject对象中去
            OneSubject oneSubject = new OneSubject();

            oneSubject.setId(oneEduSubjectObject.getId());
            oneSubject.setTitle(oneEduSubjectObject.getTitle());

            // 代码简写(把oneEduSubject值复制到oneSubject中去【要求两个类的复制注入的属性名一致】)
            // BeanUtils.copyProperties(oneEduSubject,oneSubject);

            oneFinalSubjectList.add(oneSubject);

            // 由于实体类中的一级分类和二级分类关系如下：所以需要嵌套
            // private List<TwoSubject> children = new ArrayList<>();

            // 定义一个List封装
            List<TwoSubject> finalTwoSubject = new ArrayList<>();

            for (int j = 0; j < twoSubjectQueryList.size(); j++) {
                EduSubject twoEduSubjectObject = twoSubjectQueryList.get(j);
                if (twoEduSubjectObject.getParentId().equals(oneEduSubjectObject.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoEduSubjectObject,twoSubject);
                    finalTwoSubject.add(twoSubject);
                }
            }
            oneSubject.setChildren(finalTwoSubject);

        }


        return oneFinalSubjectList;
    }


}
